// pages/businessTravelRequest/businessTravelRequest.js
// pages/addove/addove.js
var dateTimePicker = require('../../utils/dateTimePicker.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		array: ['请选择', '中国', '巴西', '日本'],
		index:0,
    date1: '请选择',
    date2: '请选择',
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    dateTimeArray2: null,
    dateTime2: null,
    startYear: 2000,
    endYear: 2050,
    loginName: app.globalData.loginName,
    applyType: "BusinessTripApproval",

    department: null,
    destination: null,
    endTime: null,
    otherRemark: null,
    remark: null,
    startTime: null
  },
  bindPickerChange: function(e) {
	console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },	
  bindDateChange1: function (e) {
    this.setData({
      date1: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj1.dateTimeArray,
      dateTime2: obj1.dateTime,
      loginName: app.globalData.loginName
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDictByCode("reimburseUnit")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //加载数据字典
  getDictByCode: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/getDictByCode', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          array: res.data.data,
        })
      }
    })
  },

  changeDateTime1(e) {
    this.setData({
      dateTime1: e.detail.value
    });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

  changeDateTime2(e) {
    this.setData({
      dateTime2: e.detail.value

    });

  },

  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr
    });
  },

  formSubmit: function (e) {
    var that = this;


    wx.showToast({
      icon: 'loading',
      title: '提交中',
      mask: true
    })

    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    // return;
    let {  startTime, endTime, department, destination, otherRemark, remark } = e.detail.value;
    if ( !startTime || !endTime || !department || !remark || !destination || !otherRemark ) {
      wx.showToast({
        title: '请填写完整数据！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }

    wx.request({
      url: app.globalData.apiUrl + '/wx/administrative/apply/addDo', //接口地址
      data: e.detail.value,
      success: function (res) {
        console.log("新增请假信息=", res)
        if (res.data.code == 0) {
          that.setData({
            date1: "请选择",
            department: null,
            destination: null,
            endTime: null,
            otherRemark: null,
            remark: null,
            startTime: null

          })
          wx.showToast({
            title: '操作成功！',
            icon: 'success',
            duration: 2000
          })

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },  

})